package com.jsb.laundry.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jsb.laundry.common.constant.LaundryModuleConstants;
import com.jsb.laundry.common.response.WebResponse;
import com.jsb.laundry.common.utils.JSBUtil;
import com.jsb.laundry.dao.LaundryIndexDao;
import com.jsb.laundry.dao.LaundryV1Dao;
import com.jsb.laundry.feign.ProductServiceFeign;
import com.jsb.laundry.model.dto.CouponRecord;
import com.jsb.laundry.model.v1.LaundryActivityVo;
import com.jsb.laundry.model.v1.LaundryCategory;
import com.jsb.laundry.model.v1.MyInfo;
import com.jsb.laundry.model.vo.LaundryCategoryVo;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@Api(tags = {"v1接口"})
@RestController
@RequestMapping("/laundryV1")
public class LaundryV1 {


    private static final Logger LOGGER = LoggerFactory.getLogger(LaundryV1.class);

    @Resource
    private ProductServiceFeign productServiceFeign;

    @Resource
    private LaundryV1Dao laundryV1Dao;
    @PostMapping("/getF")
    public WebResponse getF() {
        WebResponse<List<LaundryCategoryVo>> allCateAndGoods = productServiceFeign.findAllCateAndGoods();
        List<LaundryCategoryVo> data = allCateAndGoods.getData();
        return WebResponse.successData(data);
    }


    /**
     * 首页数据接口
     *
     * @return
     */
    @PostMapping("/getIndexDataV1")
    public WebResponse getIndexDataV1() {
        LOGGER.info("获取首页数据");
        Map returnMap = new ConcurrentHashMap();

        /**
         * 业务处理
         */
        //获取banner
//        laundryActivityModuleDao.getLaundryActivityBanner();

        //获取分类
        List<LaundryCategory> var0 = laundryV1Dao.findCate1();
        LOGGER.info("获取分类");
        //活动专区
        List<LaundryActivityVo> var1 = laundryV1Dao.getLaundryActivity1(LaundryModuleConstants.LAUNDRY_ACTIVITY_ID);
        LOGGER.info("活动专区");
        //奢侈品专区
        List<LaundryActivityVo> var2 = laundryV1Dao.getLaundryActivity1(LaundryModuleConstants.LUXURY_MODULE_ID);
        LOGGER.info("奢侈品专区");
        //快洗专区
        List<LaundryActivityVo> var3 = laundryV1Dao.getLaundryActivity1(LaundryModuleConstants.QUICK_WASH_MODULE_ID);
        LOGGER.info("快洗专区");
        //热买专区图
        List<LaundryActivityVo> var4 = laundryV1Dao.getLaundryHotActivity();
        LOGGER.info("热买专区图");
        returnMap.put("laundryCateModule", var0);
        returnMap.put("activityBanner", var1.size()==0?"":var1.get(0).getActivityBanner());
        returnMap.put("laundryActivityModule", var1);
        returnMap.put("hotModule", var4);

        //奢侈品、快洗专区
        List list = new ArrayList();
        //奢侈品专区组装
        Map varMap0 = new ConcurrentHashMap();
        varMap0.put("activityCateId", var2.size()==0?"":var2.get(0).getActivityCateId());
        varMap0.put("activityBanner", var2.size()==0?"":var2.get(0).getActivityBanner());
        varMap0.put("luxuryGoodsModule", var2);
        //快洗专区组装
        Map varMap1 = new ConcurrentHashMap();
        varMap1.put("activityCateId",var3.size()==0?"":var3.get(0).getActivityCateId());
        varMap1.put("activityBanner",var3.size()==0?"":var3.get(0).getActivityBanner());
        varMap1.put("luxuryGoodsModule",var3);

        list.add(varMap0);
//        list.add(varMap1);
        returnMap.put("laundryModule", list);

        //返回数据格式
        return WebResponse.successData(returnMap);
    }


    /**
     * 朕的查询 (userId)
     *
     * @param data
     * @return
     */
    @PostMapping("/getList")
    public WebResponse findByMy(@RequestBody String data){
        LOGGER.info("userId为:"+data);
        JSONObject jsonObject = JSONObject.parseObject(data);
        String userId = jsonObject.getString("userId");
        if (JSBUtil.isEmptyString(userId)) {
            return WebResponse.failOfParameter();
        }

        MyInfo byMy = new MyInfo();
        Date vip2Date = byMy.getVip2Date();
        // 合作会员没有过期时Vip2才赋值
        if (!JSBUtil.isEmptyObject(vip2Date) && vip2Date.after(new Date())) {
            byMy.setVip2(JSBUtil.dateFormat(vip2Date));
        }

        return WebResponse.successData(byMy);
    }


    @PostMapping("/promotion/count")
    public WebResponse count(@RequestBody String data) {
        JSONObject jsonObject = JSON.parseObject(data);
        String userId = jsonObject.getString("userId");
        if (JSBUtil.isEmptyString(userId)) {
            HashMap hashMap = new HashMap();
            hashMap.put("bycCunt", "0");
            //积累返现
            hashMap.put("toBeWithdrawn", "0.0");
            //现可提取
            hashMap.put("withdrawable", "0.0");
            return WebResponse.successData(hashMap);
        }

        int bycCunt = laundryV1Dao.findBycCunt(userId);

        //积累返现
        String totalAmountToBeWithdrawn = laundryV1Dao.sumWithdrawalAmount(userId);
        if (totalAmountToBeWithdrawn == null) {
            totalAmountToBeWithdrawn = "0";
        }
        //总额
        String totalWithdrawalAmount = laundryV1Dao.totalWithdrawalAmount(userId);
        if (totalWithdrawalAmount == null) {
            totalWithdrawalAmount = "0";
        }
        String AmountToBeWithdrawn = JSBUtil.turnDoubleStr(totalAmountToBeWithdrawn);
        String WithdrawalAmount = JSBUtil.turnDoubleStr(totalWithdrawalAmount);
        HashMap hashMap = new HashMap();
        hashMap.put("bycCunt", bycCunt);
        //积累返现
        hashMap.put("toBeWithdrawn", AmountToBeWithdrawn);
        //现可提取
        hashMap.put("withdrawable", WithdrawalAmount);
        return WebResponse.successData(hashMap);
    }





}
